<template>
  <div class="page">
    <div class="login-box">
      <div class="main">
        <el-form>
          <el-form-item action="#">
            <el-input
              v-model="username"
              size="small"
              style="width: 200px"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="password"
              type="password"
              size="small"
              style="width: 200px"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          class="loginButton"
          style="
            background-color: white;
            color: black;
            width: 150px;
            margin: auto;
            border: none;
          "
          size="small"
          @click="login"
          >Login</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>

export default {

  data() {
    return {
      username: null,
      password: null,
      routes:[
                    { 
                      path:'/dashbaord',
                      name:'控制台',
                    
                    },
                          { 
                            path:'/list',
                            name:'第一',
                             
                          },{ 
                            path:'/ggdd',
                            name:'第二',
                            children:[
                              { 
                              path:'/about',
                              name:'第三',
                            
                            }
                            ]
                          },{
                      
                      path:'/tt',
                      name:'项目',
                      children:[
                          { 
                            path:'/set',
                            name:'设置',
                             
                          }
                      
                      ]   
                    }
            ],
      
    };
  },
  created() {

    if(this.$store.state.routeList.length != 0){ 

      this.$store.commit('removeRoute')
      sessionStorage.removeItem('storeCache')
      
    }
    
  },

  methods: {
    login() {
      if (!this.username) {
        this.$message({ message: "请输入用户名", type: "error" });
        return;
      }

      if (!this.password) {
        this.$message({ message: "请输入密码", type: "error" });
        return;
      }

      this.routes = this.deep(this.routes ,'');

        this.$router.addRoutes(this.routes);

      this.$store.commit("addRoute", this.routes);
      this.$router.replace({name:this.routes[0].name});
    },

    deep(data ,prevent) {
      let arr = [];
      
      data.forEach((item) => {
        if (!Array.isArray(item.children)) {
          item.component = require(`@/views${item.path}/index.vue`).default;
          this.$router.addRoute(prevent,item)
          arr.push(item);
        } else {
          item.children = this.deep(item.children , item.path);
         this.$router.addRoute(prevent,item)
          arr.push(item);
        }
      });

      return arr;
    },

  },
};
</script>

<style scoped>
.page {
  height: 100vh;
  background-color: #999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  height: 60%;
  width: 70%;
  background: #ff2d2d;
  border-radius: 20px;
  box-shadow: 0 5px 20px 5px #666;
  display: flex;
  align-items: center;
  justify-content: center;
}

.main {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
.loginButton:hover {
  background: powderblue;
}
</style>